package com.itheima.edu.info.manager.thread;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.entry.ServerEntry;
import com.itheima.edu.info.manager.service.LoginService;
import com.itheima.edu.info.manager.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;

public class StudentRunnable implements Runnable {
    Socket socket;
    private StudentService studentService = new StudentService();

    public StudentRunnable(Socket socket) {
        this.socket = socket;
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(ServerEntry.class);

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String message = br.readLine();
            String[] split = message.split(",");
            LOGGER.info("接收到客户端的请求.........本次任务指令是:[" + split[0] + "]");
            switch (split[0]) {
                case "[0]":
                    login(split[1], split[2]);
                    break;
                case "[1]":
                    addStudent(split[1], split[2], split[3], split[4]);
                    break;
                case "[2]":
                    deleteStudent(split[1]);
                    break;
                case "[3]":
                    updateStudent(split[1], split[2], split[3], split[4]);
                    break;
                case "[4]":
                    findAllStudent();
                    break;
                case "[5]":
                    isExist(split[1]);
                    break;
                default:
                    System.out.println("服务器正忙..........");
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //登录
    private void login(String id, String password) throws IOException {
        LoginService loginService = new LoginService();
        boolean loginFlag = loginService.login(id, password);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(loginFlag + "");
        bw.flush();
        socket.shutdownOutput();
        LOGGER.info("将[0]指令查询到的数据返回给客户端");
        socket.close();

    }

    //添加学生
    private void addStudent(String s1, String s2, String s3, String s4) throws IOException {
        StudentService studentService = new StudentService();
        Student student = new Student(s1, s2, s3, s4);
        boolean addFlag = studentService.addStudent(student);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(addFlag + "");
        bw.flush();
        socket.shutdownOutput();
        LOGGER.info("将[1]指令查询到的数据返回给客户端");
        socket.close();


    }
    //删除学生
    private void deleteStudent(String id) throws IOException {
        studentService.deleteStudent(id);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("删除成功");
        bw.flush();
        socket.shutdownOutput();
        LOGGER.info("将[2]指令查询到的数据返回给客户端");
        socket.close();


    }

    //修改学生
    private void updateStudent(String id, String name, String age, String birthday) throws IOException {
        Student student = new Student(id, name, age, birthday);
        studentService.updateStudent(student, id);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("修改成功");
        bw.flush();
        socket.shutdownOutput();
        LOGGER.info("将[3]指令查询到的数据返回给客户端");
        socket.close();
    }

    //查询学生
    private void findAllStudent() throws Exception {
        StudentService studentService = new StudentService();
        Student[] allStudent = studentService.findAllStudent();
        ArrayList<Student> list = new ArrayList<>();
        Collections.addAll(list, allStudent);
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(list);
        oos.flush();
        socket.shutdownOutput();
        LOGGER.info("将[4]指令查询到的数据返回给客户端");
        socket.close();


    }

    //判断学号是否存在
    public void isExist(String id) throws IOException {
        StudentService studentService = new StudentService();
        boolean exists = studentService.isExists(id);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(exists + "");
        bw.flush();
        socket.shutdownOutput();
        LOGGER.info("将[5]指令查询到的数据返回给客户端");
        socket.close();
    }

}
